package com.zdsoft.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.zdsoft.entity.Article;
import com.zdsoft.entity.ArticleInfo;
import com.zdsoft.mapper.IArticleInfoMapper;
import com.zdsoft.mapper.IArticleMapper;
import com.zdsoft.service.IArticleService;
import com.zdsoft.vo.ArticleUpdateVO;
import com.zdsoft.vo.ArticleVO;
@Service
@Transactional
public class ArticleService implements IArticleService {
	@Autowired
	IArticleMapper articleMapper;
	@Autowired
	IArticleInfoMapper infoMapper;

	@Override
	public List<ArticleVO> getArticleList(Integer page, Integer size, String title) {
		
		Map<String,Object> params =new HashMap<>();
		params.put("size",size);
		params.put("page", size*(page-1));
		params.put("title", title);
		return 	articleMapper.getArticleList(params);
		
	}

	@Override
	public Long getTotal(String title) {
		Map<String,Object> map=new HashMap<>();
		map.put("title",title);
		return articleMapper.getTotal(map);
	}

	@Override
	public int del(Article article) {
		if(article==null){
			return 0;
		}
		articleMapper.del(article.getId());
		infoMapper.del(article.getInfoId());
		return 1;
	}

	@Override
	public void updateArticle(ArticleUpdateVO av) {
		int row =infoMapper.update(av.getContent(),av.getInfoId());
		row =articleMapper.update(av);
	}

	@Override
	public ArticleUpdateVO getArticleUpdateVOById(Long id) {
		return articleMapper.findVOById(id);
		
	}

	@Override
	public void addArticle(ArticleVO article) {
		ArticleInfo articleInfo=new ArticleInfo();
		articleInfo.setContent(article.getContent());
		
		infoMapper.saveInfo(articleInfo);
		Article a =new Article();
		a.setTitle(article.getTitle());
		a.setInfoId(articleInfo.getId());
			articleMapper.addArticle(a);
	}

}
